package com.jacky.javafx.service.impl;

import com.jacky.javafx.AppUserRepository;
import com.jacky.javafx.entities.AppUserEntity;
import com.jacky.javafx.entities.AppUserViewModel;
import com.jacky.javafx.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Service
public class AppUserServiceImpl implements AppUserService {

    private static final Integer DEFAULT_PAGE_SIZE = 10;
    private static final Integer DEFAULT_CURR_PAGE = 0;

    @Autowired
    private AppUserRepository repository;

//    public AppUserServiceImpl(AppUserRepository repository) {
//        this.repository = repository;
//    }

    @Override
    public Page<AppUserViewModel> findByPage(int pageSize, int currentPage, String sortStr) {
        PageRequest pr = PageRequest.of(currentPage, pageSize);
        Page<AppUserEntity> page = repository.findAll(pr);
        return page.map(AppUserViewModel::new);
    }

    @Override
    public Optional<AppUserEntity> findById(Long id) {
        return repository.findById(id);
    }
}
